package com.orangelife.mobile.web;

import android.os.Handler;
import android.os.Message;

import com.curry.android.util.ToastHelper;

import java.lang.ref.WeakReference;

/**
 * <p/>
 * Created by 吴格非 on 2016-8-24.
 * <p/>
 *
 * @author 吴格非
 * @since v1.0.0
 */
public class WebHandler extends Handler {
    private WeakReference<WebActivity> weakReference = null;

    public WebHandler(WebActivity activity) {
        weakReference = new WeakReference<>(activity);
    }

    public void send(int what) {
        obtainMessage(what).sendToTarget();
    }

    public void send(int what, Object obj) {
        obtainMessage(what, obj).sendToTarget();
    }

    @Override
    public void handleMessage(Message msg) {
        WebActivity act = weakReference.get();
        if (act == null) {
            return;
        }
        switch (msg.what) {
            case WebWhat.IMAGE_LOAD_COMPLETED:/*下载图片*/
                ToastHelper.getInstance()._toast("图片下载完成，保存于" + String.valueOf(msg.obj));
                break;
            case WebWhat.HIDE_TILTE:/*隐藏标题栏*/
                act.handleTitleBar(WebWhat.HIDE_TILTE, null);
                break;
            case WebWhat.SHOW_TITLE1:/*设置标题*/
                act.handleTitleBar(WebWhat.SHOW_TITLE1, String.valueOf(msg.obj));
                break;
            case WebWhat.LOAD_URL:/*加载URL*/
                act.wvDesc.loadUrl(String.valueOf(msg.obj));
                break;
            case WebWhat.SHOW_RIGHT_ICON:
                act.handleTitleBar(WebWhat.SHOW_RIGHT_ICON, null);
                break;
            case WebWhat.HIDE_RIGHT_ICON:
                act.handleTitleBar(WebWhat.HIDE_RIGHT_ICON, null);
                break;
            case WebWhat.SHOW_TITLE2:
                act.handleTitleBar(WebWhat.SHOW_TITLE2, String.valueOf(msg.obj));
                break;
            case WebWhat.GO_BACK:
                if (act.wvDesc.canGoBack()) {
                    act.wvDesc.goBack();
                }
                break;
            case WebWhat.SHOW_LOADING:
                act.showDialog();
                break;
            case WebWhat.HIDE_LOADING:
                act.closeDialog();
                break;
            case WebWhat.FINISH:
                act.finish();
                break;
            case WebWhat.JUMP:
                act.jump(String.valueOf(msg.obj));
                break;
            case WebWhat.JUMP_CERT:
                act.jumpCert();
                break;
            case WebWhat.SEND_FUNCTION_JSON:
                act.setFunctionJson(String.valueOf(msg.obj));
                break;
            case WebWhat.SEND_FUNCTION_JSON2:
                act.invokeJavaScriptFunction2(String.valueOf(msg.obj));
                break;
        }
    }
}
